using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Business_Layer;

public class ShoppingCart: DataTable
{
    public ShoppingCart()
	{
		this.Columns.Add("ProductID", typeof(int)).DefaultValue = -1;
        this.Columns.Add("ProductName", typeof(string)).DefaultValue = "";
        this.Columns.Add("UnitPrice", typeof(double)).DefaultValue = 0;
        this.Columns.Add("Quantity", typeof(int)).DefaultValue = 1;
        this.Columns.Add("Discount", typeof(double)).DefaultValue = 0;
        this.Columns.Add("Amount", typeof(double), "UnitPrice*Quantity*(1-Discount)");

        this.PrimaryKey = new DataColumn[] { this.Columns["ProductID"] };
	}

    public void Add(int ProductID, int Quantity)
    {
        this.AddProduct(ProductID, Quantity, false);
    }
    public void AddBonus(int ProductID, int Quantity)
    {
        this.AddBonus(ProductID, Quantity, false);
    }

    public void Update(int ProductID, int Quantity)
    {
        this.AddProduct(ProductID, Quantity, true);
    }

    public void Remove(int ProductID)
    {
        try
        {
            DataRow Product = this.Rows.Find(ProductID);
            this.Rows.Remove(Product);
        }
        catch
        {
            Console.WriteLine("Product not found !");
        }
    }

    public double Total
    {
        get
        {
            object value = this.Compute("SUM(Amount)", "");
            return value == DBNull.Value ? 0 : (double)value;
        }
    }

    public int Count
    {
        get
        {
            return this.Rows.Count;
        }
    }

    private void AddProduct(int ProductID, int Quantity, bool Update)
    {
        try
        {
            DataRow Item = this.Rows.Find(ProductID);
            Item["Quantity"] = Quantity + (Update ? 0 : (int)Item["Quantity"]);
        }
        catch
        {
            //String sql = "SELECT ProductID, ProductName, UnitPrice, " + Quantity + " as Quantity, 0 AS Discount FROM Products WHERE ProductID=@ProductID";
            //Database.Fill(this, sql, "@ProductID", ProductID);
            DataTable x = Product_BUS.createProductForCart(ProductID, Quantity);

            this.Rows.Add(x.Rows[0]["ID"], x.Rows[0]["Name"], x.Rows[0]["Price"], Quantity, 0);
   
        }
    }
    private void AddBonus(int ProductID, int Quantity, bool Update)
    {
        try
        {
            DataRow Item = this.Rows.Find(ProductID);
            Item["Quantity"] = Quantity + (Update ? 0 : (int)Item["Quantity"]);
        }
        catch
        {

            DataTable x = Bonus_BUS.createBonusForCart(ProductID, 1);
            //ID,Name,score,total
            this.Rows.Add(x.Rows[0][1], x.Rows[0][2], x.Rows[0][4], x.Rows[0][3], 0);

        }
    }

}
